package com.pucheng.bracelet.ui.activity.heartrate

import android.os.Bundle
import androidx.activity.viewModels
import androidx.lifecycle.Observer
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.pucheng.bracelet.R
import com.pucheng.bracelet.app.App
import com.pucheng.bracelet.app.base.BaseHistoryToDateActivity
import com.pucheng.bracelet.app.base.BaseHistoryToListActivity
import com.pucheng.bracelet.app.database.repository.BPRepository
import com.pucheng.bracelet.app.database.repository.HertRateRepository
import com.pucheng.bracelet.data.bean.BloodPressureBean
import com.pucheng.bracelet.data.bean.HertRateBean
import com.pucheng.bracelet.data.bean.Sao2Bean
import com.pucheng.bracelet.viewmodel.blood_pressure.BPModelFactory
import com.pucheng.bracelet.viewmodel.blood_pressure.BloodPressureViewModel
import com.pucheng.bracelet.viewmodel.heartrate.HertRateModelFactory
import com.pucheng.bracelet.viewmodel.heartrate.HertRateViewModel
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel

class HertRateHistoryActivity : BaseHistoryToListActivity<BaseViewModel, HertRateBean>() {

    private var historyLists = mutableListOf<HertRateBean>()
    private lateinit var mAdapter: BaseQuickAdapter<HertRateBean,BaseViewHolder>
    private val hrViewModel: HertRateViewModel by viewModels { HertRateModelFactory(
        HertRateRepository((application as App).database.hertRateDao())
    ) }

    override fun titleResId(): Int = R.string.hr_history

    override fun initView(savedInstanceState: Bundle?) {

        mAdapter = object : BaseQuickAdapter<HertRateBean, BaseViewHolder>(R.layout.item_list_history,historyLists) {
            override fun convert(holder: BaseViewHolder, item: HertRateBean) {
                holder.setTextColorRes(R.id.tv_history_content,R.color.heartrate_bg).setText(R.id.tv_history_content, item.name+getString(R.string.hr_unit))
                    .setText(R.id.tv_history_time, item.measuredDate)
            }
        }
        super.initView(savedInstanceState)
    }

    override fun getHistoryAdapter(): BaseQuickAdapter<HertRateBean, BaseViewHolder> = mAdapter
    override fun createObserver() {
        super.createObserver()
        hrViewModel.apply {

            allData.observe(this@HertRateHistoryActivity, Observer {
                it.let {
                    historyLists.clear()
                    historyLists.addAll(it as MutableList<HertRateBean>)
                    mAdapter.notifyDataSetChanged()
                }
            })
        }
    }
}